<?php
class Route{

    private static $_instance;
    public $controller;
    public $action;
    public $params;

    private function __construct(){
        $uri = $_SERVER['REQUEST_URI'];
        $uri = $this->phraseUri($uri);
    }

    public static function get_instance(){
        if(self::$_instance == null){
            self::$_instance = new Route();
        }
        return self::$_instance;
    }

    public function getRoute(){
        return array(
            'controller'=>$this->controller,
            'action'=>$this->action,
            'params'=>$this->params
        );
    }

    private function phraseUri($uri){
        $config = Basic::app()->getConfigs();
        $uri = substr(strstr($uri,'r='),2);
        $match = explode('/',$uri);
        $this->controller =(@$match[0]!=NULL)?@$match[0]:$config['default']['controller'];
        $this->action = (@$match[1]!=NULL)?@$match[1]:$config['default']['action'];
        if(@$match[2] != NULL){

            unset($match[0]);
            unset($match[1]);
            $keys = array();
            $values = array();
            foreach($match as $k=>$v){
                if($k % 2 == 0){
                    $keys[]=$v;
                }else{
                    $values[]=$v;
                }
            }
            $this->params = array_combine($keys,$values);
        }else{
            $this->params = array();
        }
    }


}